package chars;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class Player {

    File file;
    public BufferedImage appearance;
    public BufferedImage walk1;
    private BufferedImage walk2;
    private BufferedImage jump;
    private BufferedImage dead1;
    public int x;
    public int y;
    public double xSpeed;
    public double ySpeed;
    public boolean jumping = false;
    public boolean dead;
    float dieSpeed;

    public Player() {
        dead = false;
        try {
            walk1 = ImageIO.read(new File("Images/player/img1.png"));
            walk2 = ImageIO.read(new File("Images/player/img2.png"));
            jump = ImageIO.read(new File("Images/player/jump.png"));
            dead1 = ImageIO.read(new File("Images/player/dead1.png"));
            appearance = walk1;
        } catch (Exception e) {
        }
        x = 135;
        y = 30;
    }

    public void die() {
        appearance = dead1;
        dead = true;
    }

    public void move(boolean[] keysDown) {
        if (dead) {
            y -= dieSpeed;
            dieSpeed += .1;
        } else {
            dieSpeed = 0;
            if (keysDown[37]) {
                if (appearance == jump) {
                    xSpeed -= .25;
                } else {
                    xSpeed--;
                }
            }
            if (keysDown[38]) {
                if (!jumping) {
                    ySpeed -= 7 + Math.abs(xSpeed / 2);
                    jumping = true;
                }
            }
            if (keysDown[39]) {
                if (appearance == jump) {
                    xSpeed += .25;
                } else {
                    xSpeed++;
                }
            }
            if (xSpeed > 3) {
                xSpeed = 3;
            }
            if (ySpeed > 3) {
                ySpeed = 3;
            }
            if (xSpeed < -3) {
                xSpeed = -3;
            }
            if (Math.abs(ySpeed) > 1) {
                appearance = jump;
            } else {
                if (Math.abs(xSpeed) > 1) {
                    appearance = walk2;
                } else {
                    appearance = walk1;
                }
            }
            if (!jumping) {
                xSpeed = xSpeed * .90;
            } else {
                xSpeed = xSpeed * .995;
            }
            ySpeed = ySpeed * .95 + .25;
            x += (int) xSpeed;
            y += (int) ySpeed;
            if (x < 0) {
                x = 0;
            }
            if (x > 600 - appearance.getWidth(null)) {
                x = 600 - appearance.getWidth(null);
            }
            if (y > 300) {
                y = 0;
            }
        }
    }

    public void draw(Graphics2D g, platformer.Panel p, boolean[] keysDown, ArrayList<platform> platforms) {
        if (appearance != jump) {
            g.drawImage(appearance, null, x, y);
        } else {
            g.drawImage(appearance, null, x - 2, y);
        }
    }
}
